<template>
  <div class="tab-list-select" tabindex="-1" @blur="onBlur">
    <ul class="t-btns">
      <li class="t-item" :class="{unfold:showSistIndex===0}" @click="selectCampus">
        <div class="t-name">{{selectedCampusName}}</div>
        <i class="u-a"></i>
      </li>
      <li class="t-item" :class="{unfold:showSistIndex===1}" @click="selectSubject">
        <div class="t-name">{{selectedSubjectName||'科目'}}</div>
        <i class="u-a"></i>
      </li>
      <li class="t-item" :class="{unfold:showSistIndex===2}" @click="selectGrade">
        <div class="t-name">{{selectedGradeName||'年级'}}</div>
        <i class="u-a"></i>
      </li>
    </ul>
    <transition-group name="up">
      <ul class="t-list" v-show="showSistIndex===0" key="0" @click="confirmSelectCampus">
        <li class="l-item" v-for="c in listDataCampus" :class="{selected:selectedCampusId===c.campus_id}" :key="c.campus_id" :data-id="c.campus_id">{{c.campus_name}}</li>
      </ul>
      <ul class="t-list" v-show="showSistIndex===1" key="1" @click="confirmSelectSubject">
        <li class="l-item" :class="{selected:!selectedSubjectId}">所有科目</li>
        <li class="l-item" v-for="s of listDataSubject" :class="{selected:selectedSubjectId===s.subject_id}" :key="s.subject_id" :data-id="s.subject_id">{{s.subject_name}}</li>
      </ul>
      <ul class="t-list" v-show="showSistIndex===2" key="2" @click="confirmSelectGrade">
        <li class="l-item" :class="{selected:!selectedGradeId}">所有年级</li>
        <li class="l-item" v-for="g of listDataGrade" :class="{selected:selectedGradeId===g.grade_id}" :key="g.grade_id" :data-id="g.grade_id">{{g.grade_name}}</li>
      </ul>
    </transition-group>
  </div>
</template>

<script>
// import axiosGeneral from '@/modules/ajax-mid/axios-general.js'
export default {
  data () {
    return {
      showSistIndex: -1,
      listDataCampus: null,
      listDataSubject: [],
      listDataGrade: [],

      selectedCampusId: undefined,
      selectedSubjectId: undefined,
      selectedGradeId: undefined,

      selectedCampusName: null,
      selectedSubjectName: null,
      selectedGradeName: null
    }
  },
  methods: {
    confirmSelectCampus (e) {
      let { target } = e
      if (target.tagName === 'LI') {
        let { id } = target.dataset
        // if (id === this.selectedCampusId) return
        this.selectedCampusId = id
        this.selectedSubjectId = this.selectedGradeId = undefined
        this.selectedSubjectName = this.selectedGradeName = null
        this.selectedCampusName = target.textContent
        this.loadDataSubjectAndGrade(id)
        this.showSistIndex = -1
        this.$emit('change', id, null, null)
      }
    },
    confirmSelectSubject (e) {
      let { target } = e
      if (target.tagName === 'LI') {
        let { id } = target.dataset
        // if (id === this.selectedSubjectId) return
        this.selectedSubjectId = id
        this.selectedSubjectName = target.textContent
        this.$emit('change', this.selectedCampusId, id, this.selectedGradeId)
        this.showSistIndex = -1
      }
    },
    confirmSelectGrade (e) {
      let { target } = e
      if (target.tagName === 'LI') {
        let { id } = target.dataset
        // if (id === this.selectedGradeId) return
        this.selectedGradeId = id
        this.selectedGradeName = target.textContent
        this.$emit('change', this.selectedCampusId, this.selectedSubjectId, id)
        this.showSistIndex = -1
      }
    },
    selectCampus (e) {
      let { showSistIndex } = this
      this.showSistIndex = showSistIndex === 0 ? -1 : 0
    },
    selectSubject () {
      let { showSistIndex } = this
      this.showSistIndex = showSistIndex === 1 ? -1 : 1
    },
    selectGrade () {
      let { showSistIndex } = this
      this.showSistIndex = showSistIndex === 2 ? -1 : 2
    },
    onBlur () {
      this.showSistIndex = -1
    },
    loadCampus (cb) {
      if (this.listDataCampus === null) {
        this.$axios.get('/lecture/v1/alliedCampuses').then(({ data }) => {
          if (data.status === 200) {
            data = data.result
            // axiosGeneral.get('/lecture/v1/alliedCampuses').then(data => {
            // if (data) {
            let d = this.listDataCampus = data.campuses
            cb && cb(d)
          } else {
            return Promise.reject(new Error(data.message))
          }
        })
      }
    },
    loadDataSubjectAndGrade (id) {
      this.$axios.get('/lecture/v1/gradeSubjectTeachers?campus_id=' + id).then(({ data }) => {
        if (data.status === 200) {
          data = data.result
          // axiosGeneral.get('/lecture/v1/gradeSubjectTeachers?campus_id=' + id).then(data => {
          // if (data) {
          this.listDataSubject = data.subjects
          this.listDataGrade = data.grades
        } else {
          return Promise.reject(new Error(data.message))
        }
      })
    },
    initSelect () {
      return new Promise((resolve, reject) => {
        this.loadCampus(d => {
          d.some(n => {
            if (n.is_self === 'true') {
              let campusId = this.selectedCampusId = n.campus_id
              this.selectedCampusName = n.campus_name
              this.loadDataSubjectAndGrade(campusId)
              resolve(campusId)
              return true
            }
          })
          if (d.length === 0) {
            reject(new Error('没有学校'))
          }
        })
      })
    }
  }
}
</script>

<style scoped>
.up-enter-active,
.up-leave-active {
  transition: 0.2s ease;
  transition-property: opacity, transform;
}
.up-enter,
.up-leave-to {
  opacity: 0;
  transform: translate3d(0, 30%, 0);
}
.tab-list-select {
  height: 49px;
  background-color: #fff;
  outline: none;
  .t-btns {
    height: 100%;
    /* display: flex; */
  }
  .t-item {
    float: left;
    width: 33%;
    border-left: 1px solid #ccc;
    text-align: center;

    font-size: 15px;
    color: #666;
    height: 25px;
    margin: 12px 0;
    line-height: 26px;
    position: relative;
    box-sizing: border-box;

    .t-name {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      padding: 0 16px 0 10px;
      position: relative;
      display: inline-block;
      max-width: 100%;
      box-sizing: border-box;

      &::after {
        content: "";
        position: absolute;
        right: 2px;
        top: 11px;
        width: 0;
        height: 0;
        border-style: solid;
        border-width: 6px 5px 0 5px;
        border-color: #929292 transparent transparent transparent;
        vertical-align: 2px;
        margin-left: 8px;
        transition: 0.3s ease;
      }
    }
    &:first-child {
      border: 0;
    }
    &:nth-child(2) {
      width: 34%;
    }

    &.unfold .t-name::after {
      transform: rotate(180deg);
    }

    .u-a {
      position: absolute;
      left: 50%;
      top: 31px;
      z-index: 2;
      margin-left: -3px;
      width: 0;
      height: 0;
      border-style: solid;
      border-width: 0 6px 6px 6px;
      border-color: transparent transparent #ccc transparent;
      display: none;
      &::after {
        content: "";
        position: absolute;
        left: -6px;
        top: 1px;
        width: 0;
        height: 0;
        border-style: solid;
        border-width: 0 6px 6px 6px;
        border-color: transparent transparent #fff transparent;
      }
    }
    &.unfold .u-a {
      display: block;
    }
  }

  .t-list {
    position: absolute;
    border-top: 1px solid #d6d6d7;
    background-color: #fff;
    z-index: 1;
    width: 100%;
    border-bottom: 2px solid #ddd;
    box-shadow: 0 10px 20px 0px rgba(0, 0, 0, 0.1);
    height: 300px;
    overflow: auto;
  }
  .l-item {
    padding: 0 15px;
    height: 50px;
    color: #666;
    font-size: 15px;
    display: flex;
    align-items: center;
    &:active,
    &.selected {
      background-color: #ebebeb;
    }
  }
}
</style>
